home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 125
/
Freelog_MarsAvril2015_No125.iso
/
Musique
/
Quod Libet
/
quodlibet-3.3.0-installer.exe
/
bin
/
quodlibet
/
util
/
copool.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2014-12-31
|
5KB
|
144 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
'''Manage a pool of routines using Python iterators.'''
from gi.repository import GLib
class _Routine(object):
def __init__(self, pool, func, funcid, priority, timeout, args, kwargs):
self.priority = priority
self.timeout = timeout
self._source_id = None
def wrap(func, funcid, args, kwargs):
for value in func(*args, **kwargs):
yield True
pool.remove(funcid)
yield False
self.source_func = wrap(func, funcid, args, kwargs).next
def paused(self):
'''If the routine is currently running'''
return self._source_id is None
paused = property(paused)
def step(self):
'''Raises StopIteration if the routine has nothing more to do'''
return self.source_func()
def resume(self):
'''Resume, if already running do nothing'''
if not self.paused:
return None
if None.timeout:
self._source_id = GLib.timeout_add(self.timeout, self.source_func, priority = self.priority)
else:
self._source_id = GLib.idle_add(self.source_func, priority = self.priority)
def pause(self):
'''Pause, if already paused, do nothing'''
if self.paused:
return None
None.source_remove(self._source_id)
self._source_id = None
class CoPool(object):
def __init__(self):
self._CoPool__routines = { }
def add(self, func, *args, **kwargs):
'''Register a routine to run in GLib main loop.
func should be a function that returns a Python iterator (e.g.
generator) that provides values until it should stop being called.
Optional Keyword Arguments:
priority -- priority to run at (default GLib.PRIORITY_LOW)
funcid -- mutex/removal identifier for this function
timeout -- use timeout_add (with given timeout) instead of idle_add
(in milliseconds)
Only one function with the same funcid can be running at once.
Starting a new function with the same ID will stop the old one. If
no funcid is given, the function itself is used. The funcid must
be usable as a hash key.
'''
funcid = kwargs.pop('funcid', func)
if funcid in self._CoPool__routines:
remove(funcid)
priority = kwargs.pop('priority', GLib.PRIORITY_LOW)
timeout = kwargs.pop('timeout', None)
print_d('Added copool function %r with id %r' % (func, funcid))
routine = _Routine(self, func, funcid, priority, timeout, args, kwargs)
self._CoPool__routines[funcid] = routine
routine.resume()
def _get(self, funcid):
if funcid in self._CoPool__routines:
return self._CoPool__routines[funcid]
raise None('no pooled routine %r' % funcid)
def remove(self, funcid):
'''Stop a registered routine.'''
routine = self._get(funcid)
routine.pause()
del self._CoPool__routines[funcid]
print_d('Removed copool function id %r' % funcid)
def remove_all(self):
'''Stop all running routines.'''
for funcid in self._CoPool__routines.keys():
self.remove(funcid)
def pause(self, funcid):
'''Temporarily pause a registered routine.'''
routine = self._get(funcid)
routine.pause()
print_d('Paused copool function id %r' % funcid)
def pause_all(self):
'''Temporarily pause all registered routines.'''
for funcid in self._CoPool__routines.keys():
self.pause(funcid)
def resume(self, funcid):
'''Resume a paused routine.'''
routine = self._get(funcid)
routine.resume()
print_d('Resumed copool function id %r' % funcid)
def step(self, funcid):
'''Force this function to iterate once.'''
routine = self._get(funcid)
return routine.step()
_copool = CoPool()
add = _copool.add
pause = _copool.pause
pause_all = _copool.pause_all
remove = _copool.remove
remove_all = _copool.remove_all
resume = _copool.resume
step = _copool.step